package com.haohan.jingyuan.customer.controller;

import cn.hutool.json.JSONUtil;
import com.haohan.jingyuan.customer.dto.req.CreateTraceRequest;
import com.haohan.jingyuan.customer.dto.req.DeleteTraceRequest;
import com.haohan.jingyuan.customer.dto.req.QueryTraceListRequest;
import com.haohan.jingyuan.customer.dto.resp.TraceInfoResp;
import com.haohan.jingyuan.customer.dto.resp.TraceListResp;
import com.haohan.jingyuan.customer.service.CustomerTraceService;
import com.haohan.jingyuan.gateway.aspect.NeedLogin;
import com.haohan.jingyuan.gateway.dto.GwRequest;
import com.haohan.jingyuan.gateway.dto.GwResponse;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * 客户追踪控制器
 *
 * @ClassName CustomerTraceController
 * @Description 客户追踪相关接口控制器
 * @Author hexijun
 * @Time 2025/01/19 10:00
 * @Version 1.0
 */
@Slf4j
@RestController
@RequiredArgsConstructor
public class CustomerTraceController {

    /**
     * 客户追踪服务
     */
    private final CustomerTraceService customerTraceService;

    /**
     * 新增追踪
     *
     * @param request 新增追踪请求
     * @return 追踪信息
     */
    @PostMapping("/customer/trace/create")
    @NeedLogin
    public GwResponse<TraceInfoResp> createTrace(@Valid @RequestBody GwRequest<CreateTraceRequest> request) {
        log.info("CustomerTraceController.createTrace BEGIN ==> {}", JSONUtil.toJsonStr(request));

        TraceInfoResp traceInfo = customerTraceService.createTrace(request.getRequestData());
        GwResponse<TraceInfoResp> response = GwResponse.success("新增追踪成功", traceInfo);

        log.info("CustomerTraceController.createTrace END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 查询追踪列表
     *
     * @param request 查询追踪列表请求
     * @return 追踪列表
     */
    @PostMapping("/customer/trace/list")
    @NeedLogin
    public GwResponse<TraceListResp> queryTraceList(@Valid @RequestBody GwRequest<QueryTraceListRequest> request) {
        log.info("CustomerTraceController.queryTraceList BEGIN ==> {}", JSONUtil.toJsonStr(request));

        TraceListResp traceList = customerTraceService.queryTraceList(request.getRequestData());
        GwResponse<TraceListResp> response = GwResponse.success("查询追踪列表成功", traceList);

        log.info("CustomerTraceController.queryTraceList END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }

    /**
     * 删除追踪
     *
     * @param request 删除追踪请求
     * @return 删除结果
     */
    @PostMapping("/customer/trace/delete")
    @NeedLogin
    public GwResponse<String> deleteTrace(@Valid @RequestBody GwRequest<DeleteTraceRequest> request) {
        log.info("CustomerTraceController.deleteTrace BEGIN ==> {}", JSONUtil.toJsonStr(request));

        boolean success = customerTraceService.deleteTrace(request.getRequestData());
        GwResponse<String> response = null;
        if (success) {
            response = GwResponse.success("删除追踪成功", null);
        } else {
            response = GwResponse.error("删除追踪失败");
        }

        log.info("CustomerTraceController.deleteTrace END ==> {}", JSONUtil.toJsonStr(response));
        return response;
    }
}
